package com.leo.quartz.common.request;

import com.leo.base.AbstractPageRequest;
import com.leo.quartz.entity.QuartzJobPO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求任务列表请求参数
 *
 * @author liusl
 * @version 1.0
 * @date 2018/12/8 17:22
 */
public class MyJobRequestParam extends AbstractPageRequest {
    private Long id;
    private String jobName;
    private String groupName;
    private String jobDesc;
    private String jobClass;
    private int status = -1;
    private String jobStatus;

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getJobDesc() {
        return jobDesc;
    }

    public void setJobDesc(String jobDesc) {
        this.jobDesc = jobDesc;
    }

    public String getJobClass() {
        return jobClass;
    }

    public void setJobClass(String jobClass) {
        this.jobClass = jobClass;
    }

    public String getJobStatus() {
        return jobStatus;
    }

    public void setJobStatus(String jobStatus) {
        this.jobStatus = jobStatus;
    }

    @Override
    public Map<String, Object> buildRule() {
        Map<String, Object> queryParams = new HashMap<>(3);
        if (StringUtils.isNotEmpty(this.jobName)) {
            queryParams.put("jobName", this.jobName);
        }
        if (StringUtils.isNotEmpty(this.groupName)) {
            queryParams.put("jobGroup", this.groupName);
        }
        if (StringUtils.isNotEmpty(this.jobStatus) && QuartzJobPO.JobStatusEnum.UN_KNOW != QuartzJobPO.JobStatusEnum.valueOf(this.jobStatus)) {
            queryParams.put("jobStatus", QuartzJobPO.JobStatusEnum.valueOf(this.jobStatus));
        }
        return queryParams;
    }

    @Override
    public PageRequest buildPageRequestBySort() {
        Sort.Order order = Sort.Order.asc("createTime");
        return PageRequest.of(page, limit, Sort.by(order));
    }
}
